package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseArrayResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.HyphenateInfo;
import com.meitianhui.convenitenbuy.module.mine.contract.MessageGroupContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.GroupBean;
import com.meitianhui.convenitenbuy.module.mine.model.GroupDetailBean;
import com.meitianhui.convenitenbuy.module.mine.model.UserUrlBean;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.MessageGroupActivity;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by amy on 2017/11/22.
 */

public class MessageGroupPresenter implements MessageGroupContract.Presenter {

    private MessageGroupContract.View mView;
    private MessageGroupActivity mContext;

    public MessageGroupPresenter(MessageGroupActivity activity, MessageGroupActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getMsgGroup() {
        JSONObject jsonObject = new JSONObject();
        try {
            HyphenateInfo hyphenateInfo =
                    (HyphenateInfo) PreferenceUtils.readObject(mContext, Constants.HYPHENATE_INFO);
            jsonObject.put("im_user_id", hyphenateInfo.getIm_user_id());
        } catch (Exception e) {
            e.printStackTrace();
        }

        MineApiDataSource.getInstance()
                .getMsgGroup(InfoProvider.getUserToken(mContext).getUser_token(),
                        "chat.getIMUserGroupList", jsonObject.toString())
                .subscribe(new ResponseObserver<BaseArrayResponse<GroupBean>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseArrayResponse<GroupBean> s) {
                        ArrayList<GroupBean> msgGroupList = s.data;
                        if (msgGroupList.size() == 0) {
                            mView.showEmpty();
                        } else {
                            mView.showMsgGroup(msgGroupList);
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

    /**
     * 通过聊天的ID拿到聊天的名字和描述
     */
    @Override
    public void getChatDetailMsg(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("group_id", id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        MineApiDataSource.getInstance()
                .getMsgGroupDetail(InfoProvider.getUserToken(mContext).getUser_token(),
                        "chat.getChatGroupDetails", jsonObject.toString())
                .subscribe(new ResponseObserver<BaseArrayResponse<GroupDetailBean>>(mContext) {
                    @Override
                    public void onSuccess(BaseArrayResponse<GroupDetailBean> response) {
                        ArrayList<GroupDetailBean> groupDetailList = response.data;
                        mView.getGroupDetailSucc(groupDetailList, id);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

    /**
     * 拿到群组成员的列表
     */
    @Override
    public void getChatMemberList(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("group_id", id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        MineApiDataSource.getInstance()
                .getMsgGroupMember(InfoProvider.getUserToken(mContext).getUser_token(),
                        "chat.getChatGroupUsers", jsonObject.toString())
                .subscribe(new ResponseObserver<BaseArrayResponse<UserUrlBean>>(mContext) {
                    @Override
                    public void onSuccess(BaseArrayResponse<UserUrlBean> response) {
                        ArrayList<UserUrlBean> userUrlList = response.data;
                        mView.getGroupMemberSucc(userUrlList, id);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

}
